package com.stm.web.controller.synchronize;

import com.stm.basicData.domain.KfToolsBox;
import com.stm.basicData.service.KfToolsBoxService;
import com.stm.common.core.controller.BaseController;
import com.stm.common.utils.DateUtils;
import com.stm.framework.util.ShiroUtils;
import com.stm.synchronize.domain.DeviceSon;
import com.stm.synchronize.mapper.DeviceSonMapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/synchronize/monitor"})
public class MonitorController
  extends BaseController
{
  private String prefix = "synchronize/monitor";
  @Autowired
  private DeviceSonMapper deviceSonMapper;
  @Autowired
  private KfToolsBoxService kfToolsBoxService;
  
  @GetMapping
  public String device(ModelMap mmap)
  {
    mmap.put("user", ShiroUtils.getSysUser());
    return this.prefix + "/monitor";
  }
  
  @ResponseBody
  @PostMapping({"/getMapData"})
  public List<KfToolsBox> getMapData(KfToolsBox kfToolsBox)
  {
    return kfToolsBoxService.getMapData(kfToolsBox);
  }
  @ResponseBody
  @PostMapping({"/getMapDataById"})
  public List<KfToolsBox> getMapDataById(KfToolsBox kfToolsBox)
  {
	  return kfToolsBoxService.getMapDataById(kfToolsBox);
  }
  
  @PostMapping({"/initCity"})
  @ResponseBody
  public List<KfToolsBox> initCity(KfToolsBox kfToolsBox) { 
	  return this.kfToolsBoxService.selectInitCity(kfToolsBox); 
  }
  
  @GetMapping({"/detail/{id}"})
  public String detail(@PathVariable("id") String id, ModelMap mmap)
  {
    DeviceSon deviceSon = this.deviceSonMapper.selectByPrimaryDevId(id);
    if (deviceSon.getTime() != null)
    {
      String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(deviceSon.getTime()) * 1000L));
      deviceSon.setTimeDate(DateUtils.parseDate(result1));
    }
    if (deviceSon.getWgzt() != null) {
      deviceSon.setWgsc(sjc(deviceSon.getWgsc()));
    }
    if (deviceSon.getSbzt() != null) {
      deviceSon.setSbsc(sjc(deviceSon.getSbsc()));
    }
    mmap.put("eviceSon", deviceSon);
    return this.prefix + "/monitor_detail";
  }
  
  private String sjc(String sc)
  {
    String str = "";
    if ((sc != null) && (!sc.equals("")))
    {
      long dateDiff1 = Long.parseLong(sc);
      
      long dateDiff = TimeUnit.MILLISECONDS.convert(dateDiff1, TimeUnit.SECONDS);
      
      double dayDiff = Math.floor(dateDiff / 86400000L);
      
      long leave1 = dateDiff % 86400000L;
      
      double hours = Math.floor(leave1 / 3600000L);
      
      long leave2 = leave1 % 3600000L;
      double minutes = Math.floor(leave2 / 60000L);
      
      long leave3 = leave2 % 60000L;
      long seconds = Math.round((float)(leave3 / 1000L));
      if (dayDiff == 0L) {
        str = hours + "小时" + minutes + "分钟";
      } else {
        str = dayDiff + "天" + hours + "小时" + minutes + "分钟";
      }
    }
    else
    {
      str = "0分钟";
    }
    return str;
  }
}
